package com.carl.zeus.web.schedule;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.carl.zeus.common.annotation.Log;
import com.carl.zeus.common.base.BaseController;
import com.carl.zeus.common.constant.BusinessCode;
import com.carl.zeus.common.constant.BusinessConstants;
import com.carl.zeus.common.constant.BusinessMsg;
import com.carl.zeus.common.util.BusinessMsgUtil;
import com.carl.zeus.common.util.Query;
import com.carl.zeus.common.util.R;
import com.carl.zeus.common.validator.ValidatorUtils;
import com.carl.zeus.model.schedule.ScheduleJob;
import com.carl.zeus.model.system.SysRole;
import com.carl.zeus.server.schedule.ScheduleJobService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时任务
 * 
 * @author zcm
 * @email 417449614@qq.com
 * @date 2016年11月28日 下午2:16:40
 */
@Controller
@RequestMapping("/sys/schedule")
public class ScheduleJobController extends BaseController {

	@Autowired
	private ScheduleJobService scheduleJobService;
	
	/**
	 * 定时任务列表
	 */
	@RequestMapping("list")
	@RequiresPermissions("sys:schedule:list")
	public String list(@RequestParam Map<String, Object> params){
		return "schedule/schedule";
	}

	@RequestMapping(value = "/listAjax", method = RequestMethod.GET)
	@ResponseBody
	public String listAjax(@RequestParam(value = "page", defaultValue = "1") int page,
					  @RequestParam(value = "limit", defaultValue = "10") int limit,
						   @RequestParam(value = "key", defaultValue = "") String key,
						   @RequestParam(value = "value", defaultValue = "") String value){
		// 查询列表数据
		Map<String, Object> map = new HashMap<>();
		List<ScheduleJob> scheduleList = scheduleJobService.listAjax(page, limit, key, value);
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", scheduleJobService.countScheduleList());
		map.put("data", scheduleList);
		return JSON.toJSONString(map);
	}

	/**
	 * 添加定时任务
	 */
	@RequestMapping("add")
	@RequiresPermissions("sys:schedule:add")
	public String add(Model model){
		model.addAttribute("schedule", new ScheduleJob());
		return "schedule/schedule-edit";
	}

	/**
	 * 添加定时任务
	 */
	@RequestMapping("edit/{id}")
	@RequiresPermissions("sys:schedule:edit")
	public String edit(@PathVariable("id") Long id, Model model){
		ScheduleJob scheduleJob = null;
		if (id != null) {
			scheduleJob = scheduleJobService.selectById(id);
		} else {
			scheduleJob = new ScheduleJob();
		}
		model.addAttribute("schedule", scheduleJob);
		return "schedule/schedule-edit";
	}

	/**
	 * 保存定时任务
	 */
	// 增加和修改
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public BusinessMsg edit(ScheduleJob scheduleJob) {
		scheduleJob.setUpdateTime(new Date());
		if (StringUtils.isNotEmpty(scheduleJob.getBeanName()) && scheduleJob.getCreateTime() == null) {
			// 创建角色
			scheduleJob.setCreateTime(new Date());
			scheduleJobService.insertOrUpdate(scheduleJob);
		}
		return BusinessMsgUtil.returnCodeMessage(BusinessCode.GLOBAL_SUCCESS);
	}

	/**
	 * 定时任务信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:schedule:info")
	@ResponseBody
	public String info(@PathVariable("id") Long id) {
		ScheduleJob schedule = scheduleJobService.selectById(id);
		return JSON.toJSONString(schedule);
	}

	/**
	 * 批量删除定时任务
	 */
	@RequestMapping("/batchFail")
	@RequiresPermissions("sys:schedule:delete")
	@ResponseBody
	public BusinessMsg batchFail(@RequestParam(value = "jobIds[]") Long[] jobIds){
		try {
			scheduleJobService.deleteBatch(jobIds);
		} catch (Exception e) {
			return BusinessMsgUtil.returnCodeMessage(BusinessCode.GLOBAL_ERROR);
		}
		return BusinessMsgUtil.returnCodeMessage(BusinessCode.GLOBAL_SUCCESS);
	}

	/**
	 * 批量立即执行任务
	 */
	@RequestMapping("/batchExecute")
	@RequiresPermissions("sys:schedule:run")
	@ResponseBody
	public BusinessMsg run(@RequestParam(value = "jobIds[]") Long[] jobIds){
		try {
			scheduleJobService.run(jobIds);
		} catch (Exception e) {
			BusinessMsgUtil.returnCodeMessage(BusinessCode.GLOBAL_ERROR);
		}
		return BusinessMsgUtil.returnCodeMessage(BusinessCode.GLOBAL_SUCCESS);
	}
	
//	/**
//	 * 立即执行任务
//	 */
//	@Log("立即执行任务")
//	@RequestMapping("/batchExecute")
//	@RequiresPermissions("sys:schedule:run")
//	public R run1(@RequestBody Long[] jobIds){
//		scheduleJobService.run(jobIds);
//		return R.ok();
//	}
	
	/**
	 * 暂停定时任务
	 */
	@RequestMapping("batchPause")
	@RequiresPermissions("sys:schedule:pause")
	@ResponseBody
	public BusinessMsg pause(@RequestParam(value = "jobIds[]") Long[] jobIds){
		try {
			scheduleJobService.pause(jobIds);
		} catch (Exception e) {
			BusinessMsgUtil.returnCodeMessage(BusinessCode.GLOBAL_ERROR);
		}
		return BusinessMsgUtil.returnCodeMessage(BusinessCode.GLOBAL_SUCCESS);
	}
	
	/**
	 * 恢复定时任务
	 */
	@RequestMapping("/batchResume")
	@RequiresPermissions("sys:schedule:resume")
	@ResponseBody
	public BusinessMsg resume(@RequestParam(value = "jobIds[]") Long[] jobIds){
		try {
			scheduleJobService.resume(jobIds);
		} catch (Exception e) {
			BusinessMsgUtil.returnCodeMessage(BusinessCode.GLOBAL_ERROR);
		}
		return BusinessMsgUtil.returnCodeMessage(BusinessCode.GLOBAL_SUCCESS);
	}

}
